package com.eipc.view;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.eipc.R;
import com.eipc.event.OnBarClickListener;

/**
 * 状态栏,可用于xml中
 * @author liaolj
 *
 */
public class Bar extends LinearLayout {
	private static boolean isSet = false;
	public Bar(Context context){
		super(context);
		mContext = context;
		initView();
	}
	
	public Bar(Context context,AttributeSet attrs){
		super(context, attrs);
		mContext = context;
		initView();              //初如化view
		/* 设置自定义的属性  */
		TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.StatusBar);
		String temp = null;
		String str;
		str = a.getString(R.styleable.StatusBar_leftText);
		if(str!=null) mLeft.setText(str);
		int image;
		image = a.getInt(R.styleable.StatusBar_rightImage,0);
		if(image!=0) mRight.setImageResource(image);
		temp = a.getString(R.styleable.StatusBar_middleText);
		if(temp!=null) mMiddle.setText(temp);
	}
	
	/**
	 * 设置右边所显示的字符
	 * @param image
	 */
	public void setRightImage(final int image){
		mRight.setImageResource(image);
	}
	
	/**
	 * 设置左边所显示的字符
	 * @param res 字符串所在的资源id
	 */
	public void setLeftImage(final int res){
		mLeft.setBackgroundResource(res);
	}
	
	public void hideLeft(){
		mLeft.setVisibility(View.GONE);
		mMiddle.setGravity(Gravity.LEFT);
	}
	
	/**
	 * 设置左边所显示的字符
	 * @param res 字符串所在的资源id
	 */
//	public void setLeftText(final int res){
//		mLeft.setText(res);
//	}
	
	/**
	 * 设置中间所显示的字符
	 * @param text 字符串
	 */
	public void setMiddleText(final String text,boolean isOn){
		mTimerTask.cancel();
		mTimer.cancel();
		if(!isSet){
			mMiddle.setText(text);
			isSet = true;
		}
		mMiddle.setSelected(true);
		mMiddle.setTextColor(isOn?android.graphics.Color.WHITE:android.graphics.Color.RED);
	}
	
	/**
	 * 设置中间所显示的字符
	 * @param text 字符串
	 */
	public void setMiddleText(final String text){
		mTimerTask.cancel();
		mTimer.cancel();
		mMiddle.setText(text);
		mMiddle.setSelected(true);
	}
	
	/**
	 * 设置中间所显示的字符
	 * @param res 字符串所在的资源id
	 */
	public void setMiddleText(final int res){
		mTimerTask.cancel();
		mTimer.cancel();
		mMiddle.setText(res);
		mMiddle.setSelected(true);
	}
	
	/**
	 * 设置事件
	 * @param listener
	 */
	public void setOnBarClickListener(final OnBarClickListener listener){
		if(listener==null) return;
		mListener = listener;
		mLeft.setOnClickListener(new OnClickListener() {		
			public void onClick(View arg0) {
				listener.onClickLeft();
			}
		});
		mRight.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				listener.onClickRight();
			}
		});
	}	
	
	public void setOnRightClickListener(OnClickListener listener){
		if(mRight!=null) mRight.setOnClickListener(listener);
	}
	
	public void setOnLeftClickListener(OnClickListener listener){
		if(mLeft!=null) mLeft.setOnClickListener(listener);
	}
	
	
	public View getBtnLeft(){
		return mLeft;
	}
	
	public View getBtnRight(){
		return mRight;
	}
	
	public OnBarClickListener getOnBarListener(){
		return mListener;
	}
	

	/**
	 * 让状态栏中间的view显示当前时间
	 */
	public void showTime(){
		mTimer.schedule(mTimerTask, 1000,1000);
	}
	
	private void initView(){
		LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TableLayout layout = (TableLayout)inflater.inflate(R.layout.bar, null);
		this.addView(layout);
		mLeft = (Button)layout.findViewById(R.id.bar_left);
		mRight = (ImageButton)layout.findViewById(R.id.bar_right);
		mMiddle = (TextView)layout.findViewById(R.id.bar_middle);
//		mMiddle.setText(getCurrentTime()); //默认字符为当前的时间
	}
	
	/**
	 * 得到当前时间
	 * @return
	 */
	private String getCurrentTime(){
		String time = "";
		Calendar c = new GregorianCalendar();
		int minute = c.get(Calendar.MINUTE);
		time=c.get(Calendar.HOUR)+":"+(minute>=10?minute:"0"+minute);
		return time;
	}
	
	private Button mLeft;          //状态栏左边的view
	private ImageButton mRight;    //状态栏右边的view
	private TextView mMiddle;      //状态栏中间的view
	private Context mContext;
	private OnBarClickListener mListener;
	private Timer mTimer = new Timer();
	private TimerTask mTimerTask = new TimerTask() {//用于对状态栏中中间的view显示时间用的timertask
		@Override
		public void run() {
			Message message = new Message();
			message.what = 1;
			mHandler.sendMessage(message);
		}
	};
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:   //更新时间
				mMiddle.setText(getCurrentTime());
				break;
			}
		};
	};
}
